package cn.maihe.elg.operation.service.system.impl;

import cn.maihe.elg.operation.exception.ErrorCode;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.model.enums.AuditStatusEnum;
import cn.maihe.elg.operation.model.enums.BusinessStatusEnum;
import cn.maihe.elg.operation.model.vo.Query;
import cn.maihe.elg.operation.repository.entity.OrgChangeHistoryDO;
import cn.maihe.elg.operation.repository.entity.OrganizationDO;
import cn.maihe.elg.operation.repository.mapper.OrgChangeHistoryMapper;
import cn.maihe.elg.operation.repository.mapper.OrganizationMapper;
import cn.maihe.elg.operation.service.system.OrgChangeHistoryService;
import cn.maihe.elg.operation.service.system.UserService;
import cn.maihe.elg.operation.supports.electronicseal.anne.AnneSignService;
import cn.maihe.elg.operation.utils.IdGenerate;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author JHWei on 2018-07-13.
 * @ 创建日期：2018-07-13
 */
@Service
@Slf4j
public class OrgChangeHistoryServiceImpl implements OrgChangeHistoryService {

    @Resource
    private OrgChangeHistoryMapper orgChangeHistoryMapper;
    @Resource
    private OrganizationMapper organizationMapper;

    @Resource
    private UserService userService;
    @Resource
    private AnneSignService anneSignService;



    /**
     * 新增机构变更
     *
     * @param orgChangeHistory 机构变更信息
     * @param organizationDO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createOrgChangeHistory(OrgChangeHistoryDO orgChangeHistory, OrganizationDO organizationDO) {
        orgChangeHistory.setGmtCreate(new Date());
        orgChangeHistory.setUserCreate(userService.getUserContext().getUsername());
        orgChangeHistory.setGmtModified(new Date());
        orgChangeHistory.setUserModified(userService.getUserContext().getUsername());
        orgChangeHistory.setId(IdGenerate.getId());
        orgChangeHistoryMapper.insert(orgChangeHistory);
        if(AuditStatusEnum.AUDIT_REJECT.getValue()==organizationDO.getAuditStatus()){
            organizationDO.setBusinessStatus(BusinessStatusEnum.CLOSED.getValue());
            organizationDO.setAuditStatus(AuditStatusEnum.AUDIT_REJECT.getValue());
            organizationMapper.updateById(organizationDO);
        }else{
            organizationDO.setBusinessStatus(BusinessStatusEnum.OPENED.getValue());
            organizationDO.setAuditStatus(AuditStatusEnum.AUDITED.getValue());
            organizationMapper.updateById(organizationDO);
//            anneSignService.createEnterprise4Org(organizationDO);
        }
    }


    /**
     * 查询机构变更列表
     * //     * @param  机构Id
     * //     * @param  当前页码
     * //     * @param  页面大小
     *
     * @return 分页机构变更数据
     */
    @Override
    public PageInfo<OrgChangeHistoryDO> findOrgChangeHistoryByPage(Query obj) {
        try {
            obj.setPageNo(obj.getPageNo() == 0 ? 1 : obj.getPageNo());
            obj.setPageSize(obj.getPageSize() == 0 ? 10 : obj.getPageSize());
            PageHelper.startPage(obj.getPageNo(), obj.getPageSize());
            PageHelper.orderBy("gmt_modified desc");
            List<OrgChangeHistoryDO> page = orgChangeHistoryMapper.findByOrgId(obj.getParameters());
            return new PageInfo<>(page);
        } catch (Exception e) {
            log.error("机构变更数据查询报错:{}",e.getMessage(),e);
            throw new SystemRuntimeException(ErrorCode.BSMSC009);
        }

    }

    /**
     * 获取机构变更详情信息
     *
     * @param id 机构变更ID
     * @return id 对应机构变更
     */
    @Override
    public OrgChangeHistoryDO getOrgChangeHistory(Long id) {
        return orgChangeHistoryMapper.selectById(id);
    }

    /**
     * 删除机构变更
     *
     * @param id 机构变更ID
     */
    @Override
    public void deleteOrgChangeHistory(Long id) {
        OrgChangeHistoryDO orgChangeHistory = orgChangeHistoryMapper.selectById(id);
        if (orgChangeHistory == null) {
            throw new SystemRuntimeException(ErrorCode.BSMCM001);
        }
        orgChangeHistoryMapper.deleteById(id);
    }

}
